package org.jeecg.modules.cosmetic.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.compress.utils.IOUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.cosmetic.entity.TFormula;
import org.jeecg.modules.cosmetic.handler.FormulaHandler;
import org.jeecg.modules.cosmetic.model.dto.FormulaDTO;
import org.jeecg.modules.cosmetic.model.dto.FormulaProductDTO;
import org.jeecg.modules.cosmetic.model.vo.FormulaModifyVO;
import org.jeecg.modules.cosmetic.model.vo.FormulaProductListVO;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.cosmetic.model.vo.FormulaSaveVO;
import org.jeecg.modules.cosmetic.model.vo.ReqIdVO;
import org.jeecg.modules.cosmetic.utils.MyValidateUtil;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.io.*;
import java.net.URLEncoder;
import java.util.List;

/**
 * @Description: 产品配方
 * @Author: jeecg-boot
 * @Date: 2022-06-30
 * @Version: V1.0
 */
@Api(tags = "产品配方")
@RestController
@RequestMapping("/api/formula")
@Slf4j
public class TFormulaController {

    @Resource
    private FormulaHandler formulaHandler;

    @ApiOperation(value = "产品配方-导入", notes = "产品配方-导入")
    @PostMapping(value = "/import")
    public Result<FormulaDTO> importExcel(HttpServletRequest request, ReqIdVO reqIdVO) throws Exception {
        return formulaHandler.importFormulaExcel((MultipartHttpServletRequest) request, reqIdVO.getId());
    }

    @ApiOperation(value = "产品配方-列表", notes = "产品配方-列表")
    @GetMapping(value = "/query_product_list")
    public Result<List<FormulaProductDTO>> queryProductList(FormulaProductListVO vo) {
        return Result.ok(formulaHandler.queryFormulaProductList(vo));
    }

    @ApiOperation(value = "产品配方-保存", notes = "产品配方-保存")
    @PostMapping(value = "/save")
    public Result<FormulaDTO> save(@RequestBody FormulaSaveVO vo) {
        MyValidateUtil.validThrow(vo);
        return formulaHandler.saveFormulaByProductId(vo);
    }

    @ApiOperation(value = "产品配方-查询", notes = "产品配方-查询")
    @GetMapping(value = "/query")
    public Result<List<TFormula>> query(@RequestParam(name = "productId") Long productId) {
        return Result.ok(formulaHandler.queryFormulaList(productId));
    }

    @ApiOperation(value = "产品配方-根据产品主键查询配方", notes = "产品配方-根据产品主键查询配方")
    @GetMapping(value = "/query_by_product_id")
    public Result<FormulaDTO> queryByProductId(@RequestParam(name = "productId") Long productId) {
        return formulaHandler.getFormulaDataByProductId(productId);
    }

    @ApiOperation(value = "产品配方-修改", notes = "产品配方-修改")
    @PostMapping(value = "/modify")
    public Result<Void> modify(@RequestBody FormulaModifyVO vo) {
        MyValidateUtil.validThrow(vo);
        return formulaHandler.modifyFormulaByPrimary(vo);
    }

    @ApiOperation(value = "产品配方-下载模版", notes = "产品配方-下载模版")
    @GetMapping(value = "/download")
    public void exportXls(HttpServletResponse response) {
        InputStream is = null;
        OutputStream os = null;
        try {
            String filename = "安评配方模版.xlsx";
            org.springframework.core.io.Resource resource = new ClassPathResource("template/" + filename);
            boolean exists = resource.exists();
            if (!exists) {
                response.setContentType("text/html; charset=UTF-8");
                PrintWriter out = response.getWriter();
                out.println("<script defer='defer' type='text/javascript'>");
                out.println("alert('文件不存在或已经被删除,请联系管理员!');");
                out.println("</script>");
                out.flush();
            }
            response.reset();
            response.setCharacterEncoding("UTF-8");
            response.setHeader("Content-Type", " application/octet-stream");
            response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(filename, "UTF-8"));
            is = resource.getInputStream();
            os = response.getOutputStream();
            IOUtils.copy(is, os);
            response.flushBuffer();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("下载模版异常:{}", e.getMessage());
        } finally {
            try {
                if (os != null) {
                    os.close();
                }
                if (is != null) {
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
